/**
 * 
 */
package carlyang;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.List;

/**
 * @author carlyang
 * 
 */
public class iotest {

	public static Path path = Paths.get("E:/adt_workplace/1.txt");
	public static Path path1 = Paths.get("E:/adt_workplace/test/bin");

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		// Path filePath = Files.createFile(path);
		/*
		 * System.out.println(path1.getFileName());
		 * System.out.println(path1.getName(0));
		 * System.out.println(path1.getParent());
		 * System.out.println(path1.getRoot());
		 */
		Charset usAscii = Charset.forName("US-ASCII");
		// 获取用户输入
/*		System.out.println("请输入要写入文件的数据：");
		Scanner myScanner = new Scanner(System.in);
		String input = myScanner.next();*/
		// 写入指定文件
		//List<String> writeLines = Arrays.asList(input);
		// byte[] bytes = new byte[]{0x31,0x32,0x33,0x34,0x35,0x36};
		//FileTest.writeFile(path, writeLines);
		long startMillis = 0;
		long stopMillis = 0;
		startMillis = System.currentTimeMillis();
		// read back
		List<String> readLines = null;
		readLines = FileTest.readFile(path);
		if (readLines != null) {
			for (String str : readLines) {
				System.out.println(str);
			}
		}
		stopMillis = System.currentTimeMillis();
		System.out.println("使用时间："+ (stopMillis-startMillis));
		
		startMillis = System.currentTimeMillis();
		int ret;
		InputStream inputStream = FileIOStreamTest.readFile(path);
		if (inputStream != null){
			do{
				ret = inputStream.read();
				System.out.print((char)ret);
			}while(ret != -1);
			inputStream.close();
		}
		stopMillis = System.currentTimeMillis();
		System.out.println("使用时间："+ (stopMillis-startMillis));
	}
}

/**
 * 
 * @author carlyang 
 * @see 一个文件中只能有一个public类
 * @see 使用普通read write方法读写文件，适合小型文件
 */
class FileTest {
	FileTest() {
		System.out.println("creat FileTest class!");
	};

	public static void writeFile(Path path, List<String> lines)
			throws IOException {
		Path file = Files.write(path, lines, StandardOpenOption.CREATE,
				StandardOpenOption.APPEND);
		System.out.println("写入文件" + file);
	}

	public static List<String> readFile(Path path) throws IOException {
		if(Files.exists(path)){
			System.out.println("读出文件" + path);
			return Files.readAllLines(path);
		}else {
			System.out.println("文件" + path + "文件不存在！");
			return null;
			//throw new IOException("文件不存在！");
		}
	}

}

/**
 * 
 * @author carlyang
 * @see 使用IO流的方式读写文件，适合中大型文件
 *
 */
class FileIOStreamTest{
	FileIOStreamTest(){
		System.out.println("creat FileIOStream class!");
	}
	
	public static InputStream readFile(Path path) throws IOException {
		if(Files.exists(path)){
			System.out.println("使用流读出文件" + path);
			return Files.newInputStream(path, StandardOpenOption.READ);
		}else {
			System.out.println("文件" + path + "文件不存在！");
			return null;
		}
	}
	
	public static void writeFile(Path path) {
		
	}
}
